﻿import QtQuick 2.7
import QtQuick.Controls 1.2
import com.loogear.http 1.0

import "../common"
import "../../js/UI.js" as Style
import "../../js/util.js" as Util
import "../PC/"

Rectangle{//在线联系人
    id:onlinefriend
    width: parent.width
    property var m_onlineFriendData
    property var m_searchData
    property bool m_isSpread: true
    property alias m_model: m_selected_id.model
    property var p_delegateHeight: m_lineValue
    property var p_checkBoxArray:new Array
    property var p_checkBoxEnableArray:new Array    
    property var m_array: [{
            key: 0,
            groupid :12,
            fatherid:"123",
            groupname  : "开发部",
            isopen:0,
            lenth:0
        }, {
            key: 1,
            groupid : 2,
            phone  : "15213700660",
            name   :  "",
            picname:""
        }]
    signal clickSignal(var value,var picname,var nickName,var name,var phone1)
    signal searchContactSignal(var value)

    ListModel{
        id:groplist
        ListElement{
            groupid:2
            groupname:"全部"
        }
    }
    ListModel{
        id:searchedContact
        ListElement{
            phone1:"1521347555"
            m_name:"回火"
            m_picture:""
            m_nicname:"哈喽"
        }

    }
    HttpHandle{
        id:serchContactpage
        onCodeSignal: {
            console.log(code)
            if(code==200){
                m_searchData = JSON.parse(returValue)
                if(m_searchData.data !== null){
                    for(var i=0;i<m_searchData.data.length;i++){
                        searchedContact.append({"phone1":m_searchData.data[i].phoneuser.m_phone,
                                                   "m_name": m_searchData.data[i].phoneuser.m_name,
                                                   "m_nicname":m_searchData.data[i].phoneuser.m_nick_name,
                                                   "m_picture":m_searchData.data[i].image})
                    }
                    searchContactSignal(searchedContact)
                }
            }
        }
    }

    HttpHandle{
        id:grouphttp
        onCodeSignal: {
            if(code ===200){
                m_onlineFriendData = JSON.parse(returValue) //获得收到的http数据
                if(m_onlineFriendData.success){
                    m_selected_id.model = groplist
                    if(m_onlineFriendData.data !== null){
                        if(m_isSpread == true && m_onlineFriendData.data.length === 1){
                            if(groplist.count<1)
                                groplist.append({"groupid":0,"groupname": "全部"})
                            m_array.splice(0,m_array.length)
                            onlinefriendcontact.model = m_array;
                            groplist.append({"groupid":m_onlineFriendData.data[0].groupId,"groupname": m_onlineFriendData.data[0].groupName})
                            m_selected_id.positionViewAtEnd()
                            grouphttp.httpRequestFun(softphoneUrlManager.getOnlineContactChildGroupAddress(),m_onlineFriendData.data[0].groupId+","+softPhoneManager.phone,"groupId,phone")
                            m_isSpread = false
                        }
                        else{
                            for(var i=0;i<m_onlineFriendData.data.length;i++){
                                m_array.splice(i,0,{key:0,groupid:m_onlineFriendData.data[i].groupId,groupname:m_onlineFriendData.data[i].groupName,isopen:0,fatherid:0})
                            }
                        }
                    }
                    onlinefriendcontact.model = m_array;
                }else if(m_onlineFriendData.phones!=null||m_onlineFriendData.childGroups!=null){
                    var  number=0
                    var iscontact=0
                    if(m_onlineFriendData.childGroups!=null){
                        if(m_onlineFriendData.childGroups.length>0)
                            iscontact=1
                        for(var i=0;i<m_onlineFriendData.childGroups.length;i++){
                            m_array.splice(number++,0,{key:0,groupid:m_onlineFriendData.childGroups[i].id,groupname:m_onlineFriendData.childGroups[i].m_group_name,isopen:0,fatherid:m_onlineFriendData.childGroups[i].m_father_group_id})
                        }
                    }
                    if(m_onlineFriendData.phones!=null){
                        for(var i=0;i<m_onlineFriendData.phones.length;i++){
                            if(m_onlineFriendData.phones[i].phoneUser!=null){
                                iscontact=1
                                m_array.splice(number++,0,{key:1,picname:m_onlineFriendData.phones[i].image,groupid:m_onlineFriendData.phones[i].phoneUser.id,
                                                   name:Util.coalesce(m_onlineFriendData.phones[i].phoneUser.m_nick_name,m_onlineFriendData.phones[i].phoneUser.m_name,""),phone:m_onlineFriendData.phones[i].phoneUser.m_phone})

                            }
                        }
                    }
                    if(iscontact==0){
                        m_array.splice(number++,0,{key:0,groupname:"无联系人"})
                    }
                    onlinefriendcontact.model = m_array;
                }
            }
        }
    }

    Rectangle{//分组头
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        width: parent.width
        height:m_lineValue
        color: "#f5f5f5"
        visible: groplist.count>1
        Image{
            id:imgunder
            anchors.left: parent.left
            anchors.leftMargin: m_lineValue*0.2
            anchors.verticalCenter: parent.verticalCenter
            width: m_lineValue*0.25
            height: m_lineValue*0.25
            source:  "qrc:/images/PC/aroow_down.svg"
        }
        ListView {
            id: m_selected_id
            width: parent.width-0.4*m_lineValue
            height: parent.height
            anchors.left: imgunder.right
            anchors.leftMargin: 0.1*m_lineValue
            anchors.right: parent.right
            anchors.rightMargin: 0.2*m_lineValue
            anchors.verticalCenter: parent.verticalCenter
            clip: true
            model: groplist
            orientation: ListView.Horizontal
            //interactive:m_selected_id.contentWidth>parent.width-0.8*m_lineValue ? true : false
            delegate: Item {
                width: childrenRect.width+0.1*m_lineValue
                //height: childrenRect.height
                anchors.verticalCenter: parent.verticalCenter
                Image {
                    id: imgarrow
                    anchors.left: parent.left
                    //anchors.leftMargin: 0.1*m_lineValue
                    visible: index!=0
                    anchors.verticalCenter: parent.verticalCenter
                    width:  index!=0 ? m_lineValue*0.25: 0
                    height: m_lineValue*0.25
                    source: "qrc:/images/aroow.svg"
                }
                AText{
                    anchors.left: imgarrow.right
                    anchors.leftMargin: 0.1*m_lineValue
                    anchors.verticalCenter: parent.verticalCenter
                    text:model.groupname
                    font.pointSize:is_PCScreen ? 13 : 16
                    color: index==groplist.count-1 ? Style.MainColor : "#323232"
                    MouseArea{
                        anchors.fill: parent
                        onClicked: {
                            if(index==0){
                                groplist.clear()
                                m_selected_id.model=groplist
                                m_array.splice(0,m_array.length)
                                grouphttp.httpRequestFun(softphoneUrlManager.getOnlineContactAddress(),softPhoneManager.phone,"phone")
                            }
                            else{
                                groplist.remove(index+1,groplist.count-index-1)
                                m_selected_id.positionViewAtEnd()
                                m_array.splice(0,m_array.length)
                                grouphttp.httpRequestFun(softphoneUrlManager.getOnlineContactChildGroupAddress(),model.groupid+","+softPhoneManager.phone,"groupId,phone")
                            }
                        }
                    }
                }
            }
        }
        Horizontal_line{
            width: parent.width
            anchors.top: parent.bottom
        }
    }
    ListView{
        id:onlinefriendcontact
        width: onlinefriend.width
        model: m_array
        clip: true
        anchors{
            top:parent.top
            topMargin: groplist.count>1 ? m_lineValue+1 : 0
            bottom: parent.bottom
            left: parent.left
            right: parent.right
        }
        //ScrollBar.vertical: ScrollBar { size: m_lineValue*2/60 }
        maximumFlickVelocity: 5000
        delegate: Item{
            width: parent.width
            height: p_delegateHeight
            Rectangle{
                anchors.fill: parent
                visible: m_array[index].key === 0
                Text{
                    id:groupnam
                    anchors.left: img.right
                    anchors.leftMargin: 0.1*m_lineValue
                    anchors.top:parent.top
                    anchors.topMargin: (m_lineValue-height)*0.5
                    font.pointSize: is_PCScreen ? 13 : 16
                    font.family: Style.fontfamily
                    color: "#323232"
                    text:qsTr("%1").arg(m_array[index].groupname)
                }
                Image {
                    id:img
                    anchors.left: parent.left
                    anchors.leftMargin: m_lineValue*0.2
                    anchors.top:parent.top
                    anchors.topMargin: (m_lineValue-height)*0.5
                    width: m_lineValue*0.25
                    height: m_lineValue*0.25
                    source:  "qrc:/images/aroow.svg"
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        if(m_array[index].groupname=="无联系人")
                            console.log("无联系人")
                        else
                            insertcontact(m_array[index].groupid,m_array[index].groupname)
                    }
                }
            }
            Appcontact{
                anchors.fill: parent
                visible: m_array[index].key === 1
                m_headSource:{
                    var str = qsTr("")
                    if(m_array[index].picname !== "" && m_array[index].picname !== undefined && m_array[index].picname !== null)
                    {
                        str = qsTr("%1").arg(m_array[index].picname)
                    }
                    return str
                }
                color: "transparent"
                m_name: qsTr("%1").arg(Util.coalesce(m_array[index].name, m_array[index].phone, m_array[index].phone))
                m_sipSource: Style.ImagePathPC+"logo1.png"
                p_checkBoxEnable:{
                    for(var i =0;i < p_checkBoxEnableArray.length ; i++){
                        if(p_checkBoxEnableArray[i] === m_array[index].phone){
                            return false
                        }
                    }
                    return true
                }
                p_checkBoxChecked:{
                    for(var i =0;i < p_checkBoxArray.length ; i++){
                        if(p_checkBoxArray[i] === m_array[index].phone){
                            return true
                        }
                    }
                    return false
                }
                onClickSignal:{
                    if(!p_checkBoxEnable){
                        return
                    }
                    onlinefriend.clickSignal(p_checkBoxChecked,m_array[index].picname,m_array[index].nickName,m_array[index].name,m_array[index].phone)
                }
            }
            SplitLine {
                width: parent.width-m_lineValue*0.4//580
                anchors {
                    left: parent.left
                    leftMargin: 0.2*m_lineValue
                    right: parent.right
                    rightMargin: 0.2*m_lineValue
                    top:parent.top
                    topMargin: m_lineValue-1
                }
            }
        }
    }
    function onCompleted(){
        searchedContact.clear()
        m_selected_id.positionViewAtEnd()
        m_array.splice(0,m_array.length)
        onlinefriendcontact.model = m_array;
        grouphttp.httpRequestFun(softphoneUrlManager.getOnlineContactAddress(),softPhoneManager.phone,"phone")
    }
    function insertcontact(groupid,groupname){
        if(groplist.count<1)
            groplist.append({"groupid":0,"groupname": "全部"})
        m_array.splice(0,m_array.length)
        onlinefriendcontact.model = m_array;
        groplist.append({"groupid":groupid,"groupname": groupname})
        m_selected_id.positionViewAtEnd()
        grouphttp.httpRequestFun(softphoneUrlManager.getOnlineContactChildGroupAddress(),groupid+","+softPhoneManager.phone,"groupId,phone")
    }
    function searchContactFun(text){
        searchedContact.clear()
        serchContactpage.httpRequestFun(softphoneUrlManager.getSearchOnlineContactAddress(),text+","+softPhoneManager.phone,"phone2,phone")
    }
    function refreshOnlineContact(){
        onlinefriendcontact.model = ""
        onlinefriendcontact.model = m_array
    }
}
